package com.hankol.community.fragment

import android.content.Intent
import androidx.lifecycle.Observer
import com.baselibrary.activity.BaseFragment
import com.baselibrary.bean.ReleaseTopicBean
import com.baselibrary.extension.checkNotNull
import com.hankol.community.R
import com.hankol.community.adapter.DynamicSearchTopicAdapter
import com.hankol.community.bean.DoyTypeData
import com.hankol.community.databinding.FragmentDynamicSearchBinding
import com.hankol.community.viewmodel.DynamicSearchTopicViewModel

/**发布动态-选择话题
 * @description
 * @author 关忆北、
 * @time 2022/4/20 4:05 下午
 */
class DynamicSearchTopicFragment(var recommendTags: DoyTypeData.DataBean) : BaseFragment() {
    private lateinit var mDataBinding: FragmentDynamicSearchBinding
    private lateinit var mViewModel: DynamicSearchTopicViewModel
    private lateinit var adapter: DynamicSearchTopicAdapter
    private var topicList:List<ReleaseTopicBean.DataBean>?=null
    override fun init() {
        mDataBinding = getDataBinding<FragmentDynamicSearchBinding>()
        mViewModel = getFragmentViewModelProvider(this).get(DynamicSearchTopicViewModel::class.java)
        adapter = DynamicSearchTopicAdapter()
        mDataBinding.rvSearchTopicInfo.adapter = adapter
        showLoadingDialog()
        mViewModel.reqSearchTopic(recommendTags.id.toLong(),0)
        mViewModel.searchTopicLD.observe(this, Observer {
            hideLoadingDialog()
            if (it.checkNotNull()) {
                topicList = it
                adapter.setNewData(it)
            }
        })
        adapter.setOnItemClickListener { adapter, view, position ->
            if (topicList.checkNotNull()){
                val intent = Intent()
                intent.putExtra("topicBean", topicList?.get(position))
                activity?.setResult(200, intent)
                activity?.finish()
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_dynamic_search
    }

}